home *** CD-ROM | disk | FTP | other *** search
- Path: daily-planet.execpc.com!usenet
- From: sprecher@execpc.com (Reginald W. Sprecher)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: Sat, 02 Mar 1996 23:00:52 GMT
- Organization: A.C.M.E Consulting
- Message-ID: <4hagqg$6je@daily-planet.execpc.com>
- References: <4gqpa1$3h9@alcor.usc.edu>
- NNTP-Posting-Host: stickleback.execpc.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- I am sure that many of you will protest about what I am about to say
- about pointers but it is my intention to try and clarify what all the
- fuss is about.
-
- First off we need to start with some definitions.
-
- 1. A pointer - a pointer is a variable which has as its purpose the
- ability to point to another variable. One thing that needs to be made
- clear here is that the size of a pointer is always the same no matter
- what type of data the pointer is pointing at ( a pointer to a char and
- an int are the same size). However, this is not and will not hold
- across machines. For example, a pointer on an pc-xt type computer
- would be different from a pentium computer. This is because the
- pointer needs to be able to hold an address to something. If the
- memory space in a target machine is 16 bits then a pointer for that
- machine is 16 bits. If the memory space on a different machine is 32
- bits then the size of the pointer is 32 bits.
-
- 2. address - all variables, including pointers and arrays, have a
- address as to where they live in memory.
-
- 3. & - the & is the 'give me the address where the indicated variable
- lives in memory' command. All variables live in memory and therefore
- have an address. This address can only be resolved at runtime.
-
- =========================================================
-
- Now lets walk through a small program. (assume for this discussion
- that we are running on a 16 bit machine, aka pc-xt.
-
- void main(void)
- {
-
- int a;
- int b[3];
- char c[4];
- char *a_pointer;
-
- a = sizeof(b);
- a = sizeof(c);
-
- a_pointer = c; // or a_pointer = &c[0]
-
- scanf("%s",c);
-
- }
-
- 1. The first four statement instructs our program to create memory
- for the indicated variables on the stack. After these instructions
- are performed our memory looks like:
-
- address (our name)
- ________
- | | 1000 a (because a is an int we need 2 bytes to store it)
- | | 1001
- -------------
- | | 1002 b[0] this is the first element of array b
- | | 1003
- -------------
- | | 1004 b[1]
- | | 1005
- -------------
- | | 1006 b[2]
- | | 1007
- -------------
- | | 1008 c[0] this is a character array
- | | 1009 c[1] ( a character is only 1 byte big)
- -------------
- | | 1010 c[2]
- | | 1011 c[3]
- -------------
- | | 1012 a_pointer (since we have assumed a
- | | 1013 16 bit machine a pointer lives in 2 bytes
- -------------
-
-
- 2. The next thing we encounter is the sizeof instructions. Be
- carefull here, sizeof looks like a C runtime function, acts like a C
- runtime function but is not a C runtime function. It is really some
- instructions to tell the compiler to calculate the size of the
- indicated variable at compile time and not at runtime. It is because
- sizeof is performed by the compiler that the sizeof an array (or any
- variable) can be determined.
-
- 3. After the sizeof are done memory looks like..
-
- address (our name)
- ________
- | | 1000 a (because a is an int we need 2 bytes to store it)
- | 4 | 1001
- -------------
- | | 1002 b[0] this is the first element of array b
- | | 1003
- -------------
- | | 1004 b[1]
- | | 1005
- -------------
- | | 1006 b[2]
- | | 1007
- -------------
- | | 1008 c[0] this is a character array
- | | 1009 c[1] ( a character is only 1 byte big)
- -------------
- | | 1010 c[2]
- | | 1011 c[3]
- -------------
- | | 1012 a_pointer (since we have assumed a
- | | 1013 16 bit machine a pointer lives in 2 bytes
- -------------
-
-
- 3) a_pointer = c; // or a_pointer = &c[0]
-
- memory after this instruction is performed.
-
- address (our name)
- ________
- | | 1000 a (because a is an int we need 2 bytes to store it)
- | 4 | 1001
- -------------
- | | 1002 b[0] this is the first element of array b
- | | 1003
- -------------
- | | 1004 b[1]
- | | 1005
- -------------
- | | 1006 b[2]
- | | 1007
- -------------
- | | 1008 c[0] this is a character array
- | | 1009 c[1] ( a character is only 1 byte big)
- -------------
- | | 1010 c[2]
- | | 1011 c[3]
- -------------
- | | 1012 a_pointer (since we have assumed a
- | 1008 | 1013 16 bit machine a pointer lives in 2 bytes
- -------------
-
- The crux of the problem.
-
- Is the name of an array a pointer? The answer to this, using our
- definition of what a pointer is, is no. The name of an array is not a
- pointer, but it is what we would call an address. This is because we
- define all arrays in terms of where they begin in memory. Therefore
- the following expressions are equal
-
- c == &c[0]
- (1008) == (1008) ----> from our memory map
-
- This fact therefore makes it possible to intermix the following array
- notation:
-
- c[1] or *(c + 1)
-
- again, c[1] gets the contents of array location 1.
- the other expression is evaulated as:
-
- *(1008 + 1)
- *(1009) ---> use 1009 as an address and gets whats there. Looking
- at our memory map you will find that c[1] as at address 1009!
-
- WARNING: The above expression is a specific interpretation of the more
- general form for a single dimension array:
-
- *(array_name + (index * size_of_data_in_array))
-
- in our example:
-
- array_name = c or 1008
- index = 1
- data_in_array = char (each data item is 1 byte big)
-
- *(1008 + (1 * 1))
- *(1008 + 1)
- etc....
-
- Which can be generalized even further for multi-dimensional arrays:
- array_name[m][n]
-
- * ( *(array_name + (m * size_of_data_in_array) ) + (n *
- size_of_data_in_array) )
-
- 4. Scanf is a function that says it wants 'a pointer' to a variable.
- What it really wants is the address of the variable where it (scanf)
- should put the data it has. That is why the following scanf
- expressions will put the data in the same place:
-
- scanf("%s",c); AND scanf("%s",a_pointer);
-
- Evaulating the expressions: c and a_pointer we get:
-
- c = 1008 (address of array called c)
- a_pointer = 1008 (address of array called c)
-
- Therefore the actual value that gets passed to the function scanf is
- 1008, or the address of the variable that should hold the data.
-
-
-
- =========================================================
-
- Missconcepetions:
-
-
- 1) Using the name of an array somehow yields or calculates or
- generates the address.
-
- This is not true. The name of the array is the address of the array.
- The operation is not a calculation or de-referencing, or ....
- I have personally never heard of this 'decay' process. If you look
- what is really happening inside the code there is no decay process.
- This is because the program always reference an array by its address
- which is the address of the first element in the array. This same
- pricincipal applies to multi-dimensional arrays as well.
-
-
- 2) &c is a pointer to something.
-
- &c is by the above definition is not a pointer, it is an address, and
- specifically it is an address to a variable called c, which is of type
- char, and whose first element is at some address.
-
- &c is the address of myarray which is:
-
- &c == c == &c[0] == 1008 (in our memory map)
-
- Any use of this information, be it calling this value that starts at
- 1008 as a single item of type character OR a collection of things of
- type chararcter is the responsiblity of the user of address (1008).
-
- 3) Pointer take on different sizes.
-
- Pointers only take on a different size when going from one platform to
- another. If you write a program and define 1000 pointers that can
- point at base data types or your own structures, the size (amount of
- memory) that the pointer varialbe it self consumes is still the same
- size. Again, the factor here is what is the address space of your
- target system.
-
- Reginald W. Sprecher
- A.C.M.E. Consulting
- Assembler, C/C++,Microprocessor,Embedded Consulting
- sprecher@execpc.com
-
-
-